/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.model.servlet;

import br.com.model.controllers.UsuarioController;
import br.com.model.entidades.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import br.com.model.dbconector.DataBaseConnector;
import java.sql.PreparedStatement;
import javax.servlet.http.HttpSession;

/**
 *
 * @author VemQueTem
 */
public class ServletControladorLogin extends HttpServlet{

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UsuarioController usrCtrl = new UsuarioController();
        Usuario usr = new Usuario();
        
        String login = req.getParameter("pUser");
	String senha = req.getParameter("pSenha");
        PrintWriter out = resp.getWriter();
		
        try {

			String sql = "select username,password from usuarios";
			PreparedStatement ps = DataBaseConnector.getInstance().getConnection().prepareStatement(sql);
			ps.executeQuery (sql);
                        ResultSet rs = ps.executeQuery();
			rs = ps.getResultSet();
			while (rs.next ()){
				login=rs.getString("username");
				senha=rs.getString("password");
			}
			rs.close ();
			ps.close ();
			}catch(Exception e){
			System.out.println("Exception is ;"+e);
                        System.out.println("SE FERROU NÃO DEU CERTO");
			}
			if(login.equals(req.getParameter("pUser")) && senha.equals(req.getParameter("pSenha"))){
				out.println(login);
                                out.println(senha);
                                
                                HttpSession sessao = req.getSession();
                                sessao.setAttribute("usuario_autenticado", login);
                                sessao.setAttribute("STATUS", "LOGADO");
                                resp.sendRedirect("main.jsp");
			}
			else {
                                resp.sendRedirect("Login.jsp");
                                System.out.println("ÃO DEU CERTO");
                        }
    }

    @Override
    public void destroy() {
        super.destroy(); //To change body of generated methods, choose Tools | Templates.
    }
    
    
    
}
